package com.macro.mall.service.impl;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.StorageObject;
import com.macro.mall.mapper.SmsFileStorageMapper;
import com.macro.mall.model.SmsFileStorage;
import com.macro.mall.model.SmsFileStorageExample;
import com.macro.mall.service.SmsFileStorageService;
import com.macro.mall.storage.StorageService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 优惠券管理Service实现类
 *
 * @author macro
 * @date 2018/8/28
 */
@Service
@Slf4j
@AllArgsConstructor
public class SmsFileStorageServiceImpl implements SmsFileStorageService {
    private final SmsFileStorageMapper smsFileStorageMapper;
    private final StorageService storageService;

    @Override
    public StorageObject createSelective(MultipartFile file) throws IOException {
        StopWatch stopWatch = new StopWatch("上传文件");
        stopWatch.start();
        StorageObject storageObject = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), file.getOriginalFilename());
        stopWatch.stop();
        stopWatch.prettyPrint(TimeUnit.MILLISECONDS);
        SmsFileStorage smsFileStorage = new SmsFileStorage();
        smsFileStorage.setFileName(storageObject.getName());
        smsFileStorage.setFileType(storageObject.getFileType());
        smsFileStorage.setFullPath(storageObject.getUrl());
        smsFileStorage.setObjId(storageObject.getObjId());
        smsFileStorage.setFileLength(file.getSize());
        smsFileStorage.setSourceType(storageObject.getSourceType());
        int inserted = smsFileStorageMapper.insertSelective(smsFileStorage);
        if (inserted > 0) {
            log.info("[" + file.getOriginalFilename() + "]文件上传成功!");
        } else {
            log.error("[" + file.getOriginalFilename() + "]上传发生错误！");
        }
        return storageObject;

    }

    @Override
    public Integer deleteByObjId(String objId) {
        SmsFileStorageExample example = new SmsFileStorageExample();
        SmsFileStorageExample.Criteria criteria = example.createCriteria();
        criteria.andObjIdEqualTo(objId);
        storageService.delete(objId);
        return smsFileStorageMapper.deleteByExample(example);
    }

    @Override
    public Integer delete(Long id) {
        SmsFileStorage smsFileStorage = smsFileStorageMapper.selectByPrimaryKey(id);
        if (ObjUtil.isNotEmpty(smsFileStorage) && StrUtil.isNotBlank(smsFileStorage.getObjId())) {
            return deleteByObjId(smsFileStorage.getObjId());
        }
        return 0;
    }

    @Override
    public List<SmsFileStorage> list(String fileName, Integer pageNum, Integer pageSize) {
        SmsFileStorageExample example = new SmsFileStorageExample();
        SmsFileStorageExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(fileName)) {
            criteria.andFileNameLike("%" + fileName + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        example.setOrderByClause("create_time desc");
        return smsFileStorageMapper.selectByExample(example);
    }

    @Override
    public SmsFileStorage detail(Long id) {
        return smsFileStorageMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(SmsFileStorage storage) {
        return smsFileStorageMapper.updateByPrimaryKey(storage);
    }

}
